package controller

import (
	"gin/common"
	"gin/response"
	"gin/service"
	"gin/validate"
	"github.com/gin-gonic/gin"
)

func SentenceNerTokenSelect(ctx *gin.Context) {
	var selectDictQuery validate.SelectSentenceNerTokenQuery

	err := ctx.ShouldBindJSON(&selectDictQuery)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	selectQueryParam := common.GenerateSelectQueryParam(selectDictQuery.Param, "log")

	total, resData, queryErr := service.SelectSentenceNerToken(selectQueryParam)

	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": gin.H{"total": total, "rows": resData}}, "success")
	}
}

// 查询单条词库
func SentenceNerTokenFind(ctx *gin.Context) {
	var selectDictQuery validate.SelectSentenceNerTokenQuery

	err := ctx.ShouldBindJSON(&selectDictQuery)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	selectQueryParam := common.GenerateSelectQueryParam(selectDictQuery.Param, "log")

	resData, queryErr := service.FindSentenceNerToken(selectQueryParam)

	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// 插入日志
func SentenceNerTokenCreate(ctx *gin.Context) {
	var addSentenceNerToken validate.AddSentenceNerTokenWrapper

	err := ctx.ShouldBindJSON(&addSentenceNerToken)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	resData, createErr := service.CreateSentenceNerToken(addSentenceNerToken.Data)
	if createErr != nil {
		// 插入失败
		response.Fail(ctx, nil, createErr.Error())
		return
	} else {
		// 插入数据成功
		addSentenceNerToken.Data.Id = resData.Id
		addSentenceNerToken.Data.Created = resData.Created
	}

	response.Success(ctx, gin.H{"data": addSentenceNerToken.Data}, "success")
}

func SentenceNerTokenUpdate(ctx *gin.Context) {
	var updateSentenceNerToken validate.UpdateSentenceNerTokenWrapper

	err := ctx.ShouldBindJSON(&updateSentenceNerToken)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	resData, createErr := service.UpdateSentenceNerToken(updateSentenceNerToken.Data)
	if createErr != nil {
		// 插入失败
		response.Fail(ctx, nil, createErr.Error())
		return
	} else {
		// 插入数据成功
		updateSentenceNerToken.Data.Id = resData.Id
		updateSentenceNerToken.Data.Created = resData.Created
	}

	response.Success(ctx, gin.H{"data": updateSentenceNerToken.Data}, "success")
}

//删除
func SentenceNerTokenDelete(ctx *gin.Context) {
	var deleteSentenceNerTokenParam validate.DeleteSentenceNerTokenParam

	err := ctx.ShouldBindJSON(&deleteSentenceNerTokenParam)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	selectQueryParam := common.GenerateFilterParam(&deleteSentenceNerTokenParam.Param, "sentence_ner_token")

	service.DeleteSentenceNerToken(&selectQueryParam)

	// 删除成功
	response.Success(ctx, nil, "success")
}
